import 'package:flutter/cupertino.dart';
import 'package:flutter_basic_learning/utils/CommonWidget.dart';
import 'package:get/get.dart';

//监听自定义 类数据
class Person {
  RxInt age = 0.obs;
  RxString name = 'GetX'.obs;
  RxBool isCheck = true.obs;
  Rx<Friend> friend = Friend().obs;
}

class Friend {
  RxInt f_age = 12.obs;
  RxString f_name = 'friend'.obs;
}

class GetXStateManagerPage extends StatelessWidget {
  GetXStateManagerPage({super.key});

  //GetX状态变量声明三种方式
  //第一种方式  .obs
  final count = 0.obs;
  final price = 0.0.obs;
  final name = 'GetX'.obs;
  final isCheck = true.obs;
  final list = <String>[].obs;
  final map = <String, dynamic>{}.obs;
  final person = Person();

  //第二种方式 Rx 规定泛型
  final count1 = Rx<int>(0);
  final price1 = Rx<double>(0.0);
  final name1 = Rx<String>('GetX');
  final isCheck1 = Rx<bool>(true);
  final list1 = Rx<List<String>>([]);
  final map1 = Rx<Map<String, dynamic>>({});

  //第三种方式 Rx{Type}
  final count2 = RxInt(0);
  final price2 = RxDouble(0.0);
  final name2 = RxString('GetX');
  final isCheck2 = RxBool(true);
  final list2 = RxList<String>([]);
  final map2 = RxMap<String, dynamic>({});

  Widget showVariable() {
    Widget body = Column(
      spacing: 10,
      children: [
        Obx(() => commonText('${count.value}')),
        Obx(() => commonText('${count1.value}')),
        Obx(() => commonText('${count2.value}')),
        Obx(() => commonText(name.value)),
        Obx(() => commonText(name1.value)),
        Obx(() => commonText(name2.value)),
        Obx(() => commonText('${person.age.value}')),
        Obx(() => commonText(person.friend.value.f_name.value)),
      ],
    );
    return body;
  }

  Widget menuButton() {
    Widget body = Row(
      spacing: 10,
      children: [
        commonButton('count + 1', () {
          count.value++;
          count1.value++;
          count2.value++;
          name.value += '1';
          name1.value += '1';
          name2.value += '1';
          person.age.value++;
          person.friend.value.f_name.value += '1';
        })
      ],
    );
    return body;
  }

  @override
  Widget build(BuildContext context) {
    return commonScaffold('GetX状态管理学习页面', [
      showVariable(),
      menuButton(),
    ]);
  }
}
